﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

//  This class is used to keep textures that will be used globally.
//  Example: Text boxes appear often in the game but the image should only have to load once.
namespace WindowsGame1
{

    class GlobalContentManager
    {
        private Dictionary<string, Texture2D> m_Textures = new Dictionary<string, Texture2D>();
        private Dictionary<string, SpriteFont> m_Fonts = new Dictionary<string, SpriteFont>();
        private Dictionary<string, SoundEffect> m_Sounds = new Dictionary<string, SoundEffect>();
        private Dictionary<string, Song> m_Music = new Dictionary<string, Song>();
        private Dictionary<string, int> m_PlayerStats = new Dictionary<string, int>();
        private GraphicsDeviceManager m_Graphics;
        public GraphicsDeviceManager Graphics
        {
            get { return m_Graphics; }
            set { m_Graphics = value; }
        }
        //private Dictionary<string, Enemy> m_Enemies = new Dictionary<string, Enemy>();
        private Random m_Random = new Random();

        private static GlobalContentManager m_Instance = new GlobalContentManager();

        private ContentManager Content;

        public static GlobalContentManager Instance
        {
            get
            {
                return m_Instance;
            }
        }

        private GlobalContentManager()
        {
            m_PlayerStats.Add("Money", 0);
            m_PlayerStats.Add("Level", 1);
            m_PlayerStats.Add("HP", 100);
            m_PlayerStats.Add("MaxHP", 100);
            m_PlayerStats.Add("MP", 100);
            m_PlayerStats.Add("MaxMP", 100);
            m_PlayerStats.Add("Experience", 0);
            m_PlayerStats.Add("ExperienceTNL", 56);
            m_PlayerStats.Add("Strength", 5);
            m_PlayerStats.Add("Defense", 1);
            m_PlayerStats.Add("Agility", 1);
            m_PlayerStats.Add("Intellect", 3);
        }

        public Texture2D Texture(string szTextureName) 
        {
            Texture2D tempTex;
            if (m_Textures.TryGetValue(szTextureName, out tempTex) == true) {
                return tempTex;
            } else {
                tempTex = Content.Load<Texture2D>(szTextureName);
                m_Textures.Add(szTextureName, tempTex);
                return tempTex;
            }
        }

        public SpriteFont Font(string szFontName)
        {
            SpriteFont tempFont;
            if (m_Fonts.TryGetValue(szFontName, out tempFont) == true)
            {
                return tempFont;
            }
            else
            {
                tempFont = Content.Load<SpriteFont>(szFontName);
                m_Fonts.Add(szFontName, tempFont);
                return tempFont;
            }
        }

        public SoundEffect Sound(string szSoundName)
        {
            SoundEffect tempSound;
            if (m_Sounds.TryGetValue(szSoundName, out tempSound) == true)
            {
                return tempSound;
            }
            else
            {
                tempSound = Content.Load<SoundEffect>(szSoundName);
                m_Sounds.Add(szSoundName, tempSound);
                return tempSound;
            }
        }

        public Song Music(string szMusicName)
        {
            Song tempMusic;
            if (m_Music.TryGetValue(szMusicName, out tempMusic) == true)
            {
                return tempMusic;
            }
            else
            {
                tempMusic = Content.Load<Song>(szMusicName);
                m_Music.Add(szMusicName, tempMusic);
                return tempMusic;
            }
        }

        public int PlayerStat(string szStat)
        {
            if (m_PlayerStats.ContainsKey(szStat))
            {
                return m_PlayerStats[szStat];
            }
            return -1;
        }
        public void PlayerStat(string szStat, int nValue)
        {
            if (m_PlayerStats.ContainsKey(szStat))
            {
                m_PlayerStats[szStat] = nValue;
            }
        }

        public void SetContent(ContentManager _Content)
        {
            Content = _Content;
        }

        public int RandomInt(int nMin, int nMax)
        {
            return m_Random.Next(nMin, nMax);
        }

        public float RandomFloat(float fMin, float fMax)
        {
            return ((float)(m_Random.Next((int)fMin*1000, (int)fMax*1000))/1000.0f);
        }

        public bool RandomBool()
        {
            return (m_Random.Next(0, 100) >= 50);
        }
    }
}
